/**
 * Created by lixiaodi on 2018/10/10.
 */
mini.parse();

var form = new mini.Form("form1");
//下拉框赋值
var groupInput = mini.getByName("groups.groupId");     //所属集团
//初始化(得到模块选择控件)
$(function(){
    groupInput.setUrl(ajaxUrl + "/GroupsController/getList.do");  //模块Url
});
//保存
function SaveData() {
    var ids = form.getData();
    var company = {
        companyId : ids.companyId,
        companyCode : ids.companyCode,
        companyName: ids.companyName,
        companyDesc : ids.companyDesc,
        logoUrl: ids.logoUrl,
        adreess : ids.adreess,
        telephone : ids.telephone,
        email : ids.email,
        createDate : ids.createDate,
        groupId :ids.groups.groupId,
        groupCode : ids.groups.groupCode
    };
    var jsonString = JSON.stringify(company);
    $.ajax({
        url : ajaxUrl + "/CompanyController/save.do",
        type: 'POST',
        data:jsonString,
        contentType : "application/json",
        success: function (data) {
            console.log(data);
            CloseWindow("save");
        },
        error: function (jqXHR, textStatus, errorThrown) {
            //alert(jqXHR.responseText);
            CloseWindow();
        }
    });
}

//标准方法接口定义(编辑)
function SetData(data) {
    if (data.action == "edit") {
        //跨页面传递的数据对象，克隆后才可以安全使用
        //data = mini.clone(data);
        console.log(data);
        $.ajax({
            url: ajaxUrl + "/CompanyController/findById.do?id=" + data.id,
            cache: false,
            success: function (data) {
                var o = mini.decode(data);
                form.setData(o.data);
                form.setChanged(false);
            }
        });
    }else if(data.action == "new"){
        //var groupIdInput = mini.getbyName("groups.groupId");
        //groupIdInput.setValue(data.groupsId);
        groupInput.setValue(data.groupsId);
    }
}

function GetData() {
    var o = form.getData();
    return o;
}
function CloseWindow(action) {
    if (action == "close" && form.isChanged()) {
        if (confirm("数据被修改了，是否先保存？")) {
            return false;
        }
    }
    if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
    else window.close();
}
function onOk(e) {
    SaveData();
}
function onCancel(e) {
    CloseWindow("cancel");
}
var groupId;
var groupText;
//当所属集团选择改变时
function onModularChanged(e) {
    groupId = groupInput.getValue();
    groupText = groupInput.getText();
    console.log(groupId,groupText)
    return groupId,groupText;
}